//
// Created by ziya on 2021/1/16.
//

#include "../include/operandStack.h"

//=====
Stack<Slot*>* OperandStack::slotStack() {
    return _stack;
}

//=====
void OperandStack::pushInt(jint val) {
    slotStack()->push(new Slot(val));
}

void OperandStack::pushInt(Slot* slot) {
    slotStack()->push(slot);
}

void OperandStack::pushDouble(double val) {
    // 将一个double拆成两个int
    int* p = (int *)&val;

    // 将两个int封装成Slot
    // push
    slotStack()->push(new Slot(*(p)));
    slotStack()->push(new Slot(*(p + 1)));
}

//=====
jint OperandStack::popInt() {
    Slot* slot = slotStack()->top();
    slotStack()->pop();

    return slot->getData();
}

double OperandStack::popDouble() {
    int data[2];

    // 取出数据
    Slot* slot1 = slotStack()->top();
    slotStack()->pop();

    Slot* slot2 = slotStack()->top();
    slotStack()->pop();

    //TODO 组合成double(因为栈是后入先出，所以组合的顺序要反过来)
    *(data) = slot2->getData();
    *(data + 1) = slot1->getData();

    return *(double *)data;
}